//
//  main.m
//  07_MRC下的省市区
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"

int main(int argc, const char * argv[]) {
#if 1
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07_MRC下的省市区/07_MRC下的省市区/area.txt";
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *province = [[Province alloc] initWithName:area];
            [allProvinceArray addObject:province];
            [province release];
        } else if (![area hasPrefix:@"   "]) {
            City *city = [[City alloc] initWithName:area];            
            Province *province = [allProvinceArray lastObject];
            [province.arr addObject:city];
            [city release];
        } else {
            Distric *distric = [[Distric alloc] initWithName:area];
            Province *province = [allProvinceArray lastObject];
            City *city = [province.arr lastObject];
            [city.arr addObject:distric];
            [distric release];
        }
    }
    NSLog(@"%@",allProvinceArray);
#endif

#if 1
    // 数组模型
    NSString *locationPath = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07_MRC下的省市区/07_MRC下的省市区/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *allLocationArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    for (NSString *cityString in allLocationArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        // firstObject 对数组越界醉了优化,不会报错只会返回空值,比array[0] 好
        TimeCity *timeCity = [[TimeCity alloc] initWithName:[city firstObject] cityNumber:[city lastObject]];
        [allCityDicArray addObject:timeCity];
        [timeCity release];
    }
    // 排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sortDescriptor]];
    //        NSLog(@"%@",allCityDicArray);
    [sortDescriptor release];
    
    NSMutableArray *finalArray = [NSMutableArray array];
    for (TimeCity *timeCity in allCityDicArray) {
        if ([timeCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province districWithTimeCity:timeCity];
            [finalArray addObject:pro];
        } else if ([timeCity.cityNumber hasSuffix:@"00"]) {
            Province *pro = [finalArray lastObject];
            City *city = [City districWithTimeCity:timeCity];
            [pro.arr addObject:city];
        } else {
            Province *pro = [finalArray lastObject];
            City *city = [pro.arr lastObject];
            Distric *distrc = [Distric districWithTimeCity:timeCity];
            [city.arr addObject:distrc];
        }
    }
    NSLog(@"%@",finalArray);
#endif       
    
    return 0;
}
